using System;
using System.Text;
using System.Collections;

namespace PersistenceObject.Framework.Expressions
{
    public class StringField : FieldAdapter
    {
        public StringField(string name) : base(name)
        {
        }

        public StringField(string name, FieldAdapter basefield) : base(name, basefield)
        {
        }

        public StringField As(string name)
        {
            return new StringField(string.Format("{0} {1}", this.Name, name), this);
        }

        public StringField At(Table table)
        {
            String[] tempArr = this.Name.Split('.');
            if (tempArr.Length > 1)
            {
                this.name = tempArr[1];
            }
            string alias = table.Alias;
            if (string.IsNullOrEmpty(alias))
            {
                alias = table.Name;
            }
            return new StringField(string.Format("{0}.{1}", alias, this.Name), this);
        }

        public Expression In(params string[] value)
        {
            return Expressions.In(this.Name, (IList)value);
        }

        public Expression In(Table table, Expression expression)
        {
            return this.In(table, this, expression);
        }

        public Expression In(Table table, StringField field, Expression expression)
        {
            Expression exp = new InTableFieldExpression(this.Name, table.Name, field.Name);
            exp.Add(new IExpression[] { expression });
            return exp;
        }

        public Expression Like(string value)
        {
            return Expressions.Like(this.Name, value);
        }

        public Expression Match(string value)
        {
            return this.Like(string.Format("%{0}%", value));
        }

        public Expression NotIn(params string[] value)
        {
            return Expressions.In(this.Name, (IList)value, true);
        }

        public Expression NotIn(Table table, Expression exp)
        {
            return this.NotIn(table, this, exp);
        }

        public Expression NotIn(Table table, StringField field, Expression expression)
        {
            InTableFieldExpression exp = new InTableFieldExpression(this.Name, table.Name, field.Name);
            exp.Add(new IExpression[] { expression });
            exp.IsNotIn = true;
            return exp;
        }

        public static StringField operator +(StringField field, StringField field1)
        {
            return new StringField(string.Format("{0}+{1}", field.Name, field1.Name), field);
        }

        public static Expression operator ==(StringField field, string value)
        {
            return Expressions.Equals(field.Name, value);
        }

        public static Expression operator >(StringField field, string value)
        {
            return Expressions.GreaterThan(field.Name, value);
        }

        public static Expression operator >=(StringField field, string value)
        {
            return Expressions.GreaterThanOrEquals(field.Name, value);
        }

        public static Expression operator !=(StringField field, string value)
        {
            return Expressions.NotEquals(field.Name, value);
        }

        public static Expression operator <(StringField field, string value)
        {
            return Expressions.LessThan(field.Name, value);
        }

        public static Expression operator <=(StringField field, string value)
        {
            return Expressions.LessThanOrEquals(field.Name, value);
        }

        public FieldValue Set(string value)
        {
            return new FieldValue(this.Name, value);
        }
    }
}

